/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package settings.collections;

import settings.collections.elements.SettingsManager;
import settings.collections.elements.Car;
import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author rbs
 */
public class CarCollection extends SettingsManager implements Serializable{

    private ArrayList<Car> cars;

    public CarCollection() {
        super("cars");
        cars = new ArrayList<Car>();
    }
    
    
    
    public void addCar(Car car)
    {
        cars.add(car);
    }
    
    public Car getCar(int id)
    {
        for (Car car: cars)
            if (car.getId() == id)
                return car;
        return null;    
    }
    
    
    public Boolean delCar(int id)
    {
        Car car = getCar(id);
        if (car != null)
        {
            cars.remove(car);
            for (Car car2: cars)
                if (car2.getId() > id)
                    car2.setId(car2.getId() - 1);
            
            return true;
        }        
        else
            return false;
    }
    
    public void changeCar(Car old_car, Car new_car)
    {
        cars.set(old_car.getId(), new_car);
        System.out.println("Car " + old_car.getId() + " changed to: " + new_car.getName() + " (" + new_car.getId() + ")");
    }

    public ArrayList<Car> getCars() {
        return cars;
    }
    
    //Debug
    public void listRemainingCars()
    {
        System.out.println("Remaining Cars:");
        for (Car car: cars)
            System.out.println(car.getId()+ "|" + car.getName() + "|" + car.getCallName() + "|" + car.getRic());
    }
}
